package com.jsb.laundry.jhh.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;


public class Open {
    public static void main(String[] args) {
        Map jsonObject = new HashMap();
        jsonObject.put("aid",627);//代理商编号
        jsonObject.put("company_name","聚申宝(上海)企业管理有限公司");
        jsonObject.put("zone_id",40);
        jsonObject.put("name","测试");
        jsonObject.put("desc","测试");
        jsonObject.put("company_rate" ,"9%");
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        jsonObject.put("batch_no" ,uuid);
        JSONArray accounts = new JSONArray();
        Map jsonObject1 = new HashMap();
        jsonObject1.put("name","王宽");
        jsonObject1.put("id_card","142724198902101256");
        jsonObject1.put("phone","142724198902101256");
        jsonObject1.put("bank_name","中国农业银行");
        jsonObject1.put("bank_num","142724198902101256");
        jsonObject1.put("account_amount",1);
        jsonObject1.put("account_no",UUID.randomUUID().toString().replaceAll("-", ""));
//        Map jsonObject2 = new HashMap();
//        jsonObject2.put("name","方少");
//        jsonObject2.put("id_card","342901198811280211");
//        jsonObject2.put("phone","18221135284");
//        jsonObject2.put("bank_name","招商银行");
//        jsonObject2.put("bank_num","6226090218869002");
//        jsonObject2.put("account_amount",1);
//        jsonObject2.put("account_no",UUID.randomUUID().toString().replaceAll("-", ""));
//        accounts.add(jsonObject1);
//        accounts.add(jsonObject2);
        //对于account进行编码
        BASE64Encoder base64Decoder = new BASE64Encoder();
        jsonObject.put("accounts", accounts);

        String encode = "";
        String encode1 = "";
        jsonObject = sortByKey(jsonObject, false);
        Set<Map.Entry<String, Object>> entries = jsonObject.entrySet();
        int count = 0;
        for (Map.Entry<String, Object> entry : entries) {
            encode1 = entry.getKey() + "=" + entry.getValue();
            if(count==0) {
                encode += encode1;
            }else{
                encode += "&"+encode1;
            }
            count++;
        }

        String md5 = Md5Utils.MD5(encode+"D5b@sVpXn^4L^0Q6x3fu8j9S6ZYH$&@&").toLowerCase();
        jsonObject.put("verify",md5);
        try {
            JSONObject jsonObject3 = HttpsUtil.doPost("https://api.lingcaiai.com/v1/open/batchTransfer/", jsonObject);
            System.out.println(jsonObject3);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static <K extends Comparable<? super K>, V> Map<K, V> sortByKey(Map<K, V> map, boolean isDesc) {
        Map<K, V> result = Maps.newLinkedHashMap();
        if (isDesc) {
            map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByKey().reversed())
                    .forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
        } else {
            map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByKey())
                    .forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
        }
        return result;
    }
}
